package com.huice.base;


import java.util.List;
import java.util.Map;

import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.xml.XmlSuite;

public class Reporter implements IReporter {

    @Override
    public void generateReport(List<XmlSuite> arg0, List<ISuite> arg1,
                               String arg2) {

        StringBuffer sb = new StringBuffer();

        int pass = 0;
        int fail = 0;
        int skip = 0;
        int total = 0;
        float rate = 0;

        Map<String, ISuiteResult> map = arg1.get(0).getResults();

        for (String key : map.keySet()) {
            pass = map.get(key).getTestContext().getPassedTests().size();
            fail = map.get(key).getTestContext().getFailedTests().size();
            skip = map.get(key).getTestContext().getSkippedTests().size();
        }


        total = pass + fail + skip;
        try {
            rate = (pass / total) * 100;
        } catch (Exception e) {
            rate = 0;
        }

        sb.append("<!DOCTYPE html>");
        sb.append("<html>");
        sb.append("<head>");
        sb.append("	<meta charset=\"UTF-8\">");
        sb.append("<title>自动化测试报告</title>");
        sb.append("</head>");
        sb.append("<body>");
        sb.append("<h1>" + arg0.get(0).getName() + "</h1>");
        sb.append("<p>" + "测试结束时间" + DateHandler.getTimeStamp("YYYY-MM-dd HH:mm:ss") + "</p>");
        sb.append("<p>成功率" + rate + "%</p>");
        sb.append("<p>用户总数" + total + "</p>");
        sb.append("<p>成功数" + pass + "</p>");
        sb.append("<p>失败数" + fail + "</p>");
        sb.append("<p>跳过数" + skip + "</p>");
        sb.append("<p>测试日志:</p>");
        for (String log : org.testng.Reporter.getOutput()) {
            sb.append("<p>" + log + "</p>");
        }


        sb.append("</body>");
        sb.append("</html>");
        String path = MyListener.REPORT_PATH + DateHandler.getTimeStamp("yyyyMMddHHmmss") + ".html";
        FileEditor.createFile(sb.toString(), path, "UTF-8");

    }

}
